package com.boliq.lol.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.boliq.lol.R;
import com.boliq.lol.adapter.MenuAdapter;
import com.boliq.lol.adapter.SettingActivity;
import com.boliq.lol.base.BaseActivity;
import com.boliq.lol.bean.MenuBean;
import com.boliq.lol.ui.news.NewsMainFragment;
import com.boliq.lol.ui.news.NewsMyselfFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import blq.ssnb.snbutil.SnbToast;

public class MainActivity extends BaseActivity {
    private DrawerLayout mDrawerLayout;

    private ImageView userHeadView;
    private View scanView;
    private View moreView;
    private TextView userNameView;
    private TextView levelView;
    private TextView userSignatureView;
    private RecyclerView menuListView;
    private MenuAdapter mMenuAdapter;
    private View settingBtn;

    private View rootContentView;
    private View drawerBtn;

    private BottomNavigationView mBottomNavigationView;
    private final SparseArray<Fragment> mFragmentCache = new SparseArray<>();

    @Override
    protected int contentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mDrawerLayout = findViewById(R.id.drawer_layout);

        settingBtn = findViewById(R.id.ll_setting_btn);
        scanView = findViewById(R.id.img_scan_btn);
        userHeadView = findViewById(R.id.img_user_head);
        moreView = findViewById(R.id.img_more_btn);
        userNameView = findViewById(R.id.tv_user_nick);
        levelView = findViewById(R.id.tv_user_level);
        userSignatureView = findViewById(R.id.tv_signature);
        menuListView = findViewById(R.id.rv_menu_list);

        rootContentView = findViewById(R.id.view_main_content);
        drawerBtn = findViewById(R.id.img_drawer_btn);
        mBottomNavigationView = findViewById(R.id.bottom_nav_view);
    }

    @Override
    protected void initData() {
        menuListView.setLayoutManager(new LinearLayoutManager(getContext()));
        mMenuAdapter = new MenuAdapter();
        menuListView.setAdapter(mMenuAdapter);
    }

    @Override
    protected void bindData() {
        super.bindData();
        userNameView.setText("Genesis曦夜");
        levelView.setText("Lv149");
        userSignatureView.setText("这个人很懒，什么都没有留下");

        List<MenuBean> menuBeans = new ArrayList<>();
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_message_24, "我的评论"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_mail_24, "与我相关"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_assignment_24, "我的订单"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_check_box_24, "我的任务"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_loyalty_24, "我的收藏"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_card_giftcard_24, "活动中心"));
        menuBeans.add(new MenuBean(R.drawable.ic_baseline_verified_user_24, "信用分"));
        mMenuAdapter.addData(menuBeans);
        userHeadView.setImageResource(R.drawable.tx);
        mBottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            Fragment fragment = getContent(item);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fl_main_content, fragment)
                    .commit();
            return true;
        });
        mBottomNavigationView.setSelectedItemId(R.id.tab_news);
    }

    private Fragment getContent(MenuItem menuItem) {
        int id = menuItem.getItemId();
        Fragment fragment = mFragmentCache.get(id);
        if (fragment == null) {
            fragment = nf(menuItem);
            mFragmentCache.put(id, fragment);
        }
        return fragment;
    }

    private Fragment nf(MenuItem menuItem) {
        Fragment fragment = new NewsMainFragment();
        switch (menuItem.getItemId()){
            case R.id.tab_news:
                fragment =new NewsMainFragment();
                break;
            case  R.id.tab_user_center:
                fragment = new NewsMyselfFragment();
                break;

        }
        //从这里点击后将参数传入到下一个界面
        //fragment.setArguments(NewsMainFragment.newArgument(menuItem.getTitle().toString()));
        return fragment;
    }


    @Override
    protected void bindEvent() {
        settingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"点击设置", Toast.LENGTH_LONG).show();
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, SettingActivity.class);
                startActivity(intent);
            }
        });
        scanView.setOnClickListener(v -> SnbToast.showSmart(getContext(), "点击扫描"));
        moreView.setOnClickListener(v -> SnbToast.showSmart(getContext(), "点击更多"));
        drawerBtn.setOnClickListener(v -> mDrawerLayout.openDrawer(GravityCompat.START));

        mDrawerLayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                float diff = drawerView.getWidth() + drawerView.getX();
                rootContentView.setTranslationX(diff);
            }
        });
    }

}